package org.example.medical.controller;

import org.example.medical.dto.VisitRecordQueryDTO;
import org.example.medical.entity.VisitRecord;
import org.example.medical.service.VisitRecordService;
import org.example.medical.utils.ApiResult;
import org.example.medical.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/visitRecords")
public class VisitRecordApiController {
    @Autowired
    private VisitRecordService visitRecordService;

    // 1. 分页查询接口
    @GetMapping
    public ApiResult<PageResult<VisitRecord>> listVisitRecords(@ModelAttribute VisitRecordQueryDTO queryDTO) {
        List<VisitRecord> records = visitRecordService.getVisitRecords(
                queryDTO.getPatientName(),
                queryDTO.getDoctorName(),
                queryDTO.getType(),
                queryDTO.getVisitTime(),
                queryDTO.getDepartment(),
                queryDTO.getPage(),
                queryDTO.getSize()
        );
        int total = records.size(); // 在实际应用中，总记录数需要通过单独的查询获取
        return ApiResult.success(new PageResult<>(records, (long)total, queryDTO.getPage(), queryDTO.getSize()));
    }

    // 2. 新增收诊记录
    @PostMapping
    public ApiResult<?> addVisitRecord(@RequestBody VisitRecord visitRecord) {
        visitRecordService.addVisitRecord(visitRecord);
        return ApiResult.success(null, "Visit record added successfully");
    }

    // 3. 更新就诊记录
    @PutMapping("/{recordId}")
    public ApiResult<?> updateVisitRecord(
            @PathVariable Long recordId,
            @RequestBody VisitRecord visitRecord
    ) {

        visitRecord.setRecordId(recordId);
        visitRecordService.updateVisitRecord(visitRecord);
        return ApiResult.success(null, "Visit record updated successfully");
    }

    // 4. 软删除就诊记录
    @DeleteMapping("/{recordId}")
    public ApiResult softDeleteVisitRecord(@PathVariable Long recordId) {
        try {
            visitRecordService.deleteVisitRecord(recordId);
            return ApiResult.success(null, "Visit record deleted successfully");
        } catch (Exception e) {
            return ApiResult.failure(400, e.getMessage());
        }
    }

    // 5. 批量软删除就诊记录
    @PostMapping("/batchDelete")
    public ApiResult batchSoftDeleteVisitRecords(@RequestBody List<Long> recordIds) {
        visitRecordService.batchSoftDeleteVisitRecords(recordIds);
        return ApiResult.success(null, "Visit records deleted successfully");

    }
}